package kr.hkit.powerjava.chapter26.dao;

import java.sql.Connection;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.util.ArrayList;

import kr.hkit.powerjava.chapter26.common.DbUtil;
import kr.hkit.powerjava.chapter26.dto.Employee;

public class EmployeeDAO implements DaoInterface<Employee> {
	private Connection connection;
	
	public EmployeeDAO(Connection connection) {
		this.connection = connection;
	}

	@Override
	public void insertItem(Employee item) throws SQLException {
		String sql = "insert into employee values(?, ?, ?, ?, ?, ?)";
		PreparedStatement pstmt = connection.prepareStatement(sql);
		pstmt.setInt(1, item.getEmpNo());
		pstmt.setString(2, item.getEmpName());
		pstmt.setString(3, item.getTitle());
		pstmt.setInt(4, item.getManager());
		pstmt.setInt(5, item.getSalary());
		pstmt.setInt(6, item.getDno());
		pstmt.executeUpdate();
		DbUtil.close(pstmt);		
	}

	@Override
	public void deleteItemByNo(Employee item) throws SQLException {
		String sql = "delete from employee where empno = ?";
		PreparedStatement pstmt = connection.prepareStatement(sql);
		pstmt.setInt(1, item.getEmpNo());
		pstmt.executeUpdate();
		DbUtil.close(pstmt);		
	}

	@Override
	public void updateItemByNo(Employee item) throws SQLException {
		String sql = "update employee set empname=?, title=?, manager=?, salary=?, dno=? where empno=?";
		PreparedStatement pstmt = connection.prepareStatement(sql);
		pstmt.setString(1, item.getEmpName());
		pstmt.setString(2, item.getTitle());
		pstmt.setInt(3, item.getManager());
		pstmt.setInt(4, item.getSalary());
		pstmt.setInt(5, item.getDno());
		pstmt.setInt(6, item.getEmpNo());
		pstmt.executeUpdate();
		DbUtil.close(pstmt);		
	}

	@Override
	public ArrayList<Employee> listItems() throws SQLException {
		ArrayList<Employee> list = new ArrayList<>();
		String sql = "select empno, empname, title, manager, salary, dno from employee";
		PreparedStatement pstmt = connection.prepareStatement(sql);
		ResultSet rs = pstmt.executeQuery();
		while(rs.next()){
			Employee em = new Employee();
			em.setEmpNo(rs.getInt(1));
			em.setEmpName(rs.getString(2));
			em.setTitle(rs.getString(3));
			em.setManager(rs.getInt(4));
			em.setSalary(rs.getInt(5));
			em.setDno(rs.getInt(6));
			list.add(em);
		}
		DbUtil.close(rs);
		DbUtil.close(pstmt);
		return list;
	}

	@Override
	public Employee searchItem(Employee item) throws SQLException {
		Employee emp = null;
		String sql = "select empno, empname, title, manager, salary, dno from employee where empno=?";
		PreparedStatement pstmt = connection.prepareStatement(sql);
		pstmt.setInt(1, item.getEmpNo());
		ResultSet rs = pstmt.executeQuery();
		
		if (rs.next()) {
			emp = new Employee();
			emp.setEmpNo(rs.getInt(1));
			emp.setEmpName(rs.getString(2));
			emp.setTitle(rs.getString(3));
			emp.setManager(rs.getInt(4));
			emp.setSalary(rs.getInt(5));
			emp.setDno(rs.getInt(6));
		}
		
		return emp;
	}

}
